package servlet;

import service.MessageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "MessageAddServlet")
public class MessageAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message = new String(request.getParameter("message").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        MessageService messageService = new MessageService();
        messageService.addMessage(message);

        request.getRequestDispatcher("MessageServlet?page=1").forward(request, response);
    }
}
